package cn.edu.stu.service;

import cn.edu.stu.bean.AjaxResult;
import cn.edu.stu.bean.MicRecord;
import cn.edu.stu.bean.MicSong;
import cn.edu.stu.bean.MicUser;
import cn.edu.stu.manager.AsyncManager;
import cn.edu.stu.manager.factory.AsyncFactory;
import cn.edu.stu.mapper.MicRecordMapper;
import cn.edu.stu.mapper.MicSongMapper;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Karl
 * @Date 2022/12/18 - 16:40
 */
@Service
@Transactional
public class RecordService {
    @Autowired
    MicRecordMapper recordMapper;
    @Autowired
    MicSongMapper songMapper;

    public void updateRecord(MicRecord record){
        if(record==null||record.getRecordId()==-1) return;
        recordMapper.updateByPrimaryKey(record);
    }

    public void deleteRecord(MicRecord record) {
        if(record!=null)
        recordMapper.deleteByPrimaryKey(record.getRecordId());
    }

    public AjaxResult deleteRecordBySongId(Integer songId, Long userId) {
        List<MicRecord> records = recordMapper.selectByUidAndSongId(songId,userId);

        int result = recordMapper.deleteByKeyAndUserId(songId, userId);
        if(result==0){
            return AjaxResult.error("删除失败，查无此歌！");
        }else if (result!=records.size()){
            return AjaxResult.error("删除错误！");
        }
        for (MicRecord record : records) {
            if("1.img".equals(record.getImgPath())) continue;
            AsyncManager.me().execute(AsyncFactory.deleteSongImg(record.getImgPath()));
        }
        return AjaxResult.success("删除成功！");
    }

    public void updateSongId(MicSong song, MicSong existSong, MicRecord record) {
        songMapper.deleteByPrimaryKey(song.getSongId());
        record.setSongId(existSong.getSongId());
        recordMapper.updateByPrimaryKey(record);
    }

    public PageInfo<Map<String,Object>> searchByFuzzyName(String name,int pageNum,int pageSize,int pageToShow) {

        long count = PageHelper.count(() -> recordMapper.selectRecordBuFuzzyName(name));
        if(count>=pageSize){
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> maps = recordMapper.selectRecordBuFuzzyName(name);
            if(maps.size()>=pageSize) return new PageInfo<>(maps,pageToShow);
        }

        PageHelper.clearPage();
        PageHelper.startPage(pageNum,pageSize);

        int len = (int) Math.floor(name.length()/2.0);
        if(len==0) len =1;
        String partName = name.substring(0, len);
        List<Map<String, Object>> newMap = recordMapper.selectRecordsWithPartName(partName,name);
        return new PageInfo<>(newMap,pageToShow);
    }

    public PageInfo<Map<String, Object>> selectByUidWithPage(long uid,int pageNum,int pageSize,int pageToShow) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> records = recordMapper.selectByUid(uid);
        return new PageInfo<>(records,pageToShow);
    }

    public List<Map<String, Object>> selectAllByUid(long uid) {
        return recordMapper.selectByUid(uid);
    }

    public PageInfo<Map<String, Object>> selectAll(int pageNum, int pageSize, int pageToShow) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> records = recordMapper.selectAllRecord();
        return new PageInfo<>(records,pageToShow);
    }
}
